#include <iostream>
using namespace std;

int main1()
{
	// 算法作答时加快效率
	// 取消c语言缓冲区 和 c++缓冲区的绑定
	ios_base::sync_with_stdio(false);
	// 取消cout cin 的绑定
	cout.tie(nullptr);
	cin.tie(nullptr);

	int i = 0;
	while (cin >> i)
	{
		cout << "fail:" << cin.fail() << endl;
		cout << "good:" << cin.good() << endl;
		cout << "bad:" << cin.bad() << endl;
		cout << "eof:" << cin.eof() << endl;
		// cout << i << endl;
	}
	cout << "fail:" << cin.fail() << endl;
	cout << "good:" << cin.good() << endl;
	cout << "bad:" << cin.bad() << endl;
	cout << "eof:" << cin.eof() << endl;

	if (cin.fail())
	{
		// 读取出错时 应该先把它清理一下 然后在把缓冲区中的错误字符除去
		// 就可以继续输入了
		cin.clear();
		char ch;
		while (cin.get(ch))
		{
			cout << ch;
		}
	}

	while (cin >> i);
	cout << "fail:" << cin.fail() << endl;
	cout << "good:" << cin.good() << endl;
	cout << "bad:" << cin.bad() << endl;
	cout << "eof:" << cin.eof() << endl;

	return 0;
}

#include <fstream>
// 文件操作 C++
int main2()
{
	// 默认是 文本读取
	//ifstream ifs("test.cpp", ios_base::in);

	// 二进制读取
	ifstream ifs("text.txt", ios_base::in | ios_base::binary);

	while (!ifs.eof())
	{
		char ch = ifs.get();
		cout << ch;
	}


	return 0;
}

// 文件写 默认是文本写入
int main3()
{

	// 默认是文本写入
	ofstream ofs("text.txt");

	// 如果要二进制写入的话
	//ofstream ofs("text.txt", ios_base::out | ios_base::binary);
	ofs << "asdfghjkl" << " " << 1 << " " << 80 << "二进制" << endl;

	return 0;
}

struct Date
{
	int _year;
	int _month;
	int _day;
};

struct Info
{
	Date d1;
	char name[20];
	int age;
};

ostream& operator<<(ostream& out, const Date& d1)
{
	out << d1._year << " " << d1._month << " " << d1._day << " ";
	return out;
}

istream& operator>>(istream& in, Date& d1)
{
	in >> d1._year >> d1._month >> d1._day;
	return in;
}

class Data
{
	friend ostream& operator<<(ostream& out, const Date& d1);
	friend istream& operator>>(istream& in, Data& d1);
public:
	Data(const char* name)
		:_filename(name)
	{}

	// 实现写入文本文件的操作
	void Write(Info& info)
	{
		ofstream ofs(_filename);
		ofs << info.name << " " << info.age << " " << info.d1 << endl;
	}

	// 实现文本读取文件的操作
	// 对于自定义类型的文件进行自己实现 读取和输出操作
	void Read(Info& info)
	{
		ifstream ifs(_filename);
		ifs >> info.name >> info.age >> info.d1;
	}

	// 二进制的写入文件的操作
	void WriteBinary(Info& info)
	{
		ofstream ofs(_filename, ios_base::out | ios_base::binary);
		ofs << info.name << " " << info.age << " " << info.d1 << endl;
	}

	// 二进制的文件读取操作
	void ReadBinary(Info& info)
	{
		ifstream ifs(_filename, ios_base::in | ios_base::binary);
		ifs >> info.name >> info.age >> info.d1;
	}

private:
	const char* _filename;
};


int main4()
{
	Info if1 = { {2024,9,25},"zhangsan",20 };
	Data data1("text.txt");
	data1.Write(if1);
	Info if2;
	data1.Read(if2);

	return 0;
}

#include <sstream>
#include <string>

ostream& operator<<(ostream& out, const Info& d1)
{
	out << d1.d1 << " " << d1.name << " " << d1.age << " ";
	return out;
}



int main()
{
	Info if1 = { {2024,9,25},"zhangsan",20 };
	string str1;

	// 把 if1 写成字符串放进str1中去 默认是覆盖放入
	// 需要写入字符串的话 直接输入这个打开方式即可
	// ostringstream sst(ios_base::out | ios_base::ate);
	ostringstream sst;
	sst << if1.name << " " << if1.age << " " << if1.d1 << endl;

	// 在写进去 把数据存在str1中
	str1 = sst.str();
	cout << str1 << endl;

	// 把str1中的数据读出来, 需要输入读取的数据字符串
	istringstream ist(str1);
	Info if2;
	// 把数据读到正确的位置
	ist >> if2.name >> if2.age >> if2.d1;

	cout << if2.name << " " << if2.age << " " << if2.d1 << endl;

	return 0;
}
